const { con, Result } = require('./common');

let Comment_C = async (commodityco_id, userco_id, comment_context, time) => {
	let res = new Result();
	let arr = [];
	let sql = `select * from comment`;
	let [rows] = await con.execute(sql);
	if (rows.length == 0) {
		let sql1 = `insert into comment (comment_id,commodity_id,userco_id,comment_context,comment_time) values(1,${commodityco_id},${userco_id},'${comment_context}','${time}')`;
		let [rows1] = await con.execute(sql1);
	} else {
		let sql1 = `insert into comment (comment_id,commodity_id,userco_id,comment_context,comment_time) values(${rows[rows.length - 1].comment_id + 1},${commodityco_id},${userco_id},'${comment_context}','${time}')`;
		let [rows1] = await con.execute(sql1);
	}
	res.msg = '发布成功';
	let sql2 = `select * from comment where commodity_id = ${commodityco_id}`;
	let [rows2] = await con.execute(sql2);
	let sql3 = `select * from user where user_id = ${rows2[rows2.length-1].userco_id}`;
	let [rows3] = await con.execute(sql3);
	arr = rows2[rows2.length-1];
	arr.user_pic = rows3[0].user_pic;
	arr.user_name = rows3[0].user_name;
	arr.user_id = userco_id;
	arr.time = rows2[0].comment_time;
	res.data = arr;
	return res;
}
let Comment_D = async(dynamicco_id,userco_id,comment_context,time)=>{
        let res = new Result();
        let sql = `select * from comment`;
	let arr =[];
        let [rows] = await con.execute(sql);
        if(rows.length == 0){
                let sql1 = `insert into comment (comment_id,dynamicco_id,userco_id,comment_context,comment_time) values(1,${dynamicco_id},${userco_id},'${comment_context}','${time}')`;
                let [rows1] = await con.execute(sql1);
        }else{
                let sql1 = `insert into comment (comment_id,dynamicco_id,userco_id,comment_context,comment_time) values(${rows[rows.length-1].comment_id+1},${dynamicco_id},${userco_id},'${comment_context}','${time}')`;
                let [rows1] = await con.execute(sql1);
        }
        res.msg = '发布成功';
	let sql2 = `select * from comment where dynamicco_id = ${dynamicco_id}`;
        let [rows2] = await con.execute(sql2);
        let sql3 = `select * from user where user_id = ${rows2[rows2.length-1].userco_id}`;
        let [rows3] = await con.execute(sql3);
        arr = rows2[rows2.length-1];
        arr.user_pic = rows3[0].user_pic;
        arr.user_name = rows3[0].user_name;
	arr.user_id = userco_id;
	arr.time = `${time}`;
        res.data = arr;
	
        return res;
}
let Comment_delete = async (comment_id) => {
	let res = new Result();
	let sql = `delete from comment where comment_id = ${comment_id}`;
	let [rows] = await con.execute(sql);
	res.data = comment_id;
	return res;
}
let Comment_get = async (userco_id) => {
	let res = new Result();
	let sql = `select * from comment where userco_id = ${userco_id}`;
	let [rows] = await con.execute(sql);
	res.data = rows;
	return res;
}

let Comment_get_commodity = async (commodity_id) => {
	let res = new Result();
	let sql = `select * from comment where commodity_id = ${commodity_id}`;
	let [rows] = await con.execute(sql);
	let rows3 = [];
	for(let i= 0;i<rows.length;i++){
		let sql2 = `select user_id,user_name,user_pic from user where user_id = ${rows[i].userco_id}`;
		let [rows2] = await con.execute(sql2);
		rows3[i] = rows2[0];
		rows3[i].comment_id = rows[i].comment_id;
		rows3[i].comment_context = rows[i].comment_context;
		rows3[i].time = rows[i].comment_time; 
	}
	res.data = rows3;
	return res;
}
let Comment_get_dynamic = async(dynamicco_id)=>{
        let res = new Result();
        let sql = `select * from comment where dynamicco_id = ${dynamicco_id}`;
        let [rows] = await con.execute(sql);
        let rows3 = [];
        for(let i= 0;i<rows.length;i++){
                let sql2 = `select user_id,user_name,user_pic from user where user_id = ${rows[i].userco_id}`;
                let [rows2] = await con.execute(sql2);
                rows3[i] = rows2[0];
                rows3[i].comment_context = rows[i].comment_context;
                rows3[i].time = rows[i].comment_time;
		rows3[i].comment_id =  rows[i].comment_id;
        }
        res.data = rows3;
        return res;
}
module.exports = { Comment_C, Comment_D, Comment_delete, Comment_get, Comment_get_commodity, Comment_get_dynamic }

